//
//  BaseTabBarVC.m
//  KuaiTu
//
//  Created by 江伟 on 2018/10/24.
//  Copyright © 2018年 WeiJiang. All rights reserved.
//

#import "BaseTabBarVC.h"
#import "BaseNavigationVC.h"

#import "ZYXHomeViewController.h"
#import "ZYXClassificationViewController.h"
#import "ZYXCourseViewController.h"
#import "ZYXVideoViewController.h"
#import "ZYXMyViewController.h"

#import "BaseTabBar.h"
#define MallClassKey   @"rootVCClassString"
#define MallTitleKey   @"title"
#define MallImgKey     @"imageName"
#define MallSelImgKey  @"selectedImageName"



@interface BaseTabBarVC ()<UITabBarControllerDelegate,BaseTabBarDelegate>

@property (nonatomic, strong) UIButton *center_Btn;

@end

@implementation BaseTabBarVC
{
    BOOL _canSelect;
    
    BOOL _animalFinish;
    
}

@synthesize canSelect = _canSelect;

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.delegate = self;
        [self.tabBar setBackgroundImage:[UIImage new]];
        self.tabBar.tintColor = selectColor;
        UIImage *image = [[UIImage alloc]init];
        image = [image imageWithColor:[UIColor redColor]];
        [self.tabBar setShadowImage:image];
        self.tabBar.translucent = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *childArray = @[
                            @{MallClassKey  : @"ZYXHomeViewController",
                              MallTitleKey  : @"首页",
                              MallImgKey    : @"shouye-un",
                              MallSelImgKey : @"shouye"},
                            
                            @{MallClassKey  : @"ZYXClassificationViewController",
                              MallTitleKey  : @"分类",
                              MallImgKey    : @"fenlei_un",
                              MallSelImgKey : @"fenlei"},
                            
//                            @{MallClassKey  : @"ZYXCourseViewController",
//                              MallTitleKey  : @"选课",
//                              MallImgKey    : @"xuanke-un",
//                              MallSelImgKey : @"xuanke"},
                            
                            @{MallClassKey  : @"ZYXVideoViewController",
                              MallTitleKey  : @"视频",
                              MallImgKey    : @"shipin-un",
                              MallSelImgKey : @"shipin"},
                            
                            @{MallClassKey  : @"ZYXMyViewController",
                              MallTitleKey  : @"我的",
                              MallImgKey    : @"wode-un",
                              MallSelImgKey : @"wode"},
                            
                            ];
    
    [childArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIViewController *vc = [NSClassFromString(dict[MallClassKey]) new];
        UITabBarItem *item = vc.tabBarItem;
        item.image = [UIImage imageNamed:dict[MallImgKey]];
        item.selectedImage = [[UIImage imageNamed:dict[MallSelImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        BaseNavigationVC *nav = [[BaseNavigationVC alloc] initWithRootViewController:vc];
        nav.tabBarItem.title = dict[MallTitleKey];
        [self addChildViewController:nav];
    }];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    //点击tabBarItem动画
    [self tabBarButtonClick:[self getTabBarButton]];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    NSLog(@"跳转登录");
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[HXLoginVC new]];
//    [self presentViewController:nav animated:YES completion:nil];
    return YES;
}

- (UIControl *)getTabBarButton {
    NSMutableArray *tabBarButtons = [[NSMutableArray alloc]initWithCapacity:0];
    
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]){
            [tabBarButtons addObject:tabBarButton];
        }
    }
    UIControl *tabBarButton = [tabBarButtons objectAtIndex:self.selectedIndex];
    
    return tabBarButton;
}

#pragma mark -- 点击动画
- (void)tabBarButtonClick:(UIControl *)tabBarButton {
    for (UIView *imageView in tabBarButton.subviews) {
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            //需要实现的帧动画,这里根据自己需求改动
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.1,@0.9,@1.0];
            animation.duration = 0.3;
            animation.calculationMode = kCAAnimationCubic;
            //添加动画
            [imageView.layer addAnimation:animation forKey:nil];
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
